package com.rt.code.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.code.dto.A1templatetableDto;
import com.rt.code.dto.PageDto;
import com.rt.code.mapper.A1templatecolumnMapper;
import com.rt.code.mapper.A1templatetableMapper;
import com.rt.code.service.intf.IA1templatetableService;
import com.rt.tmpt.dto.Result;
import com.rt.tmpt.service.impl.DataLimitService;
import com.rt.tmpt.utils.DateTool;
import com.rt.code.bean.A1templatetable;
import com.rt.easyjava.common.config.dozer.MyMapper;
import com.rt.code.utils.page.MyPageUtils;

import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wxy on 2016/7/28.
 */
@Service(value="a1templatetableService")
@Transactional(propagation = Propagation.REQUIRED)
public class A1templatetableService extends ServiceImpl<A1templatetableMapper, A1templatetable> implements IA1templatetableService {


    @Autowired
    private DataLimitService dataLimitService;
    @Autowired
    private MyMapper mapper;
    @Autowired
    private MyPageUtils myPageUtils;
    @Resource
    private A1templatetableMapper a1templatetableMapper;
    @Resource
    private A1templatecolumnMapper a1templatecolumnMapper;


    @Override
    public Result<A1templatetableDto> pagination(A1templatetableDto a1templatetableDto, PageDto pageDto) {
        //分页查询
        Result<A1templatetableDto> result = myPageUtils.pagination(
                a1templatetableDto,pageDto,
                A1templatetable.class,
                A1templatetableDto.class,
                a1templatetableMapper,
                false,null);

        return result;
    }

    /**
    * 分布查询，有数据边界控制功能
    * @param a1templatetableDto
    * @param pageDto
    * @param paramMap
    * @return
    */
    @Override
    public Result<A1templatetableDto> pagination(A1templatetableDto a1templatetableDto, PageDto pageDto, Map<String,Object> paramMap) {
        //数据边界控制参数
        Map<String,Object> paramMap1 = dataLimitService.getLimitParam(paramMap);

        //分页查询
        Result<A1templatetableDto> result = myPageUtils.pagination(
                a1templatetableDto,pageDto,
                A1templatetable.class,
                A1templatetableDto.class,
                a1templatetableMapper,
                paramMap1,
                false,null);

        return result;
    }


    @Override
    public Result<A1templatetableDto> selectAll() {
        Result<A1templatetableDto> result = new Result();
        //排序
        PageDto pageDto = new PageDto();
        pageDto.setSortColumn("id");
        pageDto.setSortOrder("desc");
        Map<String,Object> mapParam = new HashMap<>();
        mapParam.put("page",pageDto);

        List<A1templatetable> list = a1templatetableMapper.mySelectAll(mapParam);
        List<A1templatetableDto> dtoList = mapper.maplist(list, A1templatetableDto.class);
        result.setList(dtoList);

        return result.success();
    }

    @Override
    public Result<A1templatetableDto> selectById(long id) {
        Result<A1templatetableDto> result = new Result();

        A1templatetable a1templatetable = a1templatetableMapper.mySelectById(id);
        if(a1templatetable == null){
            return result.fault("记录不存在");
        }
        A1templatetableDto dto = mapper.map(a1templatetable,A1templatetableDto.class);
        result.setObj(dto);

        return result.success();
    }



    @Override
    public Result<A1templatetableDto> selectByKeyRandom(String keyRandom) {
        Result<A1templatetableDto> result = new Result();


        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("keyrandom", keyRandom);
        queryWrapper.eq("deleteflag", 0);
        A1templatetable a1templatetable = a1templatetableMapper.selectOne(queryWrapper);

        if(a1templatetable == null){
            return result.fault("记录不存在");
        }
        A1templatetableDto dto = mapper.map(a1templatetable,A1templatetableDto.class);
        result.setObj(dto);

        return result.success();
    }


    /**
        返回ID值
    */
    @Override
    public Result<A1templatetableDto> addData(A1templatetableDto a1templatetableDto){
        Result<A1templatetableDto> result = new Result();

        //封闭数据
        A1templatetable a1templatetable = mapper.map(a1templatetableDto, A1templatetable.class);
        a1templatetable.setCreatedate(DateTool.getCurTimestamp());

        //调用mapper
        long i = a1templatetableMapper.myInsert(a1templatetable);
        if(i==0){
            return result.fault("插入失败");
        }

        A1templatetableDto dto = mapper.map(a1templatetable,A1templatetableDto.class);
        result.setObj(dto);

        return result.success();
    }

    /**
        返回ID值
    */
    @Override
    public Result<A1templatetableDto> editData(A1templatetableDto a1templatetableDto){
        Result<A1templatetableDto> result = new Result();

        //封装数据
        A1templatetable a1templatetable = mapper.map(a1templatetableDto, A1templatetable.class);
        a1templatetable.setModifydate(DateTool.getCurTimestamp());

        //调用mapper
        long i = a1templatetableMapper.myUpdateById(a1templatetable);
        if(i==0){
            return result.fault("编辑数据失败");
        }

        A1templatetableDto dto = mapper.map(a1templatetable,A1templatetableDto.class);
        result.setObj(dto);

        return result.success();
    }

    /**
     * edit config tree json
     * @param a1templatetableDto
     * Long a1templatetableId, String treeJson
     * @return
     */
    @Override
    public Result<A1templatetableDto> editTreeJsonById(A1templatetableDto a1templatetableDto){
        Result<A1templatetableDto> result = new Result();

        //调用mapper
        A1templatetable a1templatetable = a1templatetableMapper.selectById(a1templatetableDto.getId());
        a1templatetable.setTreeJson(a1templatetableDto.getTreeJson());
        long i = a1templatetableMapper.updateById(a1templatetable);
        if(i==0){
            return result.fault("编辑数据失败");
        }
        A1templatetableDto dto = mapper.map(a1templatetable,A1templatetableDto.class);
        result.setObj(dto);

        return result.success();
    }

    /**
    伪删除记录，返回ID值
    */
    @Override
    public Result deleteById(long id, String adminMan){
        Result result = new Result();

        Map<String,Object> mapParameter = new HashMap();
        mapParameter.put("id",id);
        mapParameter.put("modifyman",adminMan);
        mapParameter.put("modifydate", DateTool.getCurTimestamp());
        a1templatetableMapper.myDeleteById(mapParameter);

        // 结连删除
        Map<String,Object> mapParameterColumn = new HashMap();
        mapParameterColumn.put("tmptTableId",id);
        mapParameterColumn.put("modifyman",adminMan);
        mapParameterColumn.put("modifydate", DateTool.getCurTimestamp());
        a1templatecolumnMapper.myDeleteByTbId(mapParameterColumn);


        return result.success();
    }





}
